package com.hqyj.springCloudAccount.controller;


import com.github.pagehelper.PageInfo;
import com.hqyj.entity.account.User;
import com.hqyj.entity.common.ResultEntity;
import com.hqyj.entity.common.SearchBean;
import com.hqyj.springCloudAccount.entity.UserVo;
import com.hqyj.springCloudAccount.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/account")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 127.0.0.1:8762/api/account/login ---- post
     * {"userName":"baiyun", "password":"123456"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    @Transactional
    public ResultEntity<User> login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     * 127.0.0.1:8762/api/account/user ---- post
     * {"userName":"baiyun", "password":"111111","email":"123456@qq.com","userImage":"/image/phoenix.jpg"}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    @Transactional
    public ResultEntity<User> addUser(@RequestBody User user){
        return userService.addUser(user);
    }


    /**
     * 127.0.0.1:8762/api/account/user ---- put
     * {"id":1,"userName":"admin", "password":"123456"}
     */
    @PutMapping(value = "/user", consumes = "application/json")
    @Transactional
    public ResultEntity<User> updateUserByUser(@RequestBody User user){
        return userService.updateUserByUser(user);
    }

    /**
     * 127.0.0.1:8762/api/account/user/2 ---- delete
     */
    @DeleteMapping(value = "/user/{id}")
    public ResultEntity<User> deleteUserById(@PathVariable int id){
        return userService.deleteUserById(id);
    }

    /**
     *127.0.0.1:8762/api/account/user/1 ---- get
     */
    @GetMapping("user/{id}")
    public User selectUserById(@PathVariable int id){
        return userService.selectUserById(id);
    }

    /**
     * 127.0.0.1:8762/api/account/users ---- post
     * {"currentPage":"1","pageSize":"5","orderBy":"id","direction":"desc","keyWord":""}
     */
    @PostMapping(value = "/users", consumes = "application/json")
    public PageInfo<User> selectAllUser(@RequestBody SearchBean searchBean) {
        return userService.selectAllUser(searchBean);
    }

    /**
     *127.0.0.1:8762/api/account/userVo/1 ---- get
     */
    @GetMapping("userVo/{id}")
    public UserVo getUserVoById(@PathVariable int id){
        return userService.getUserVoById(id);
    }


    /**
     * 127.0.0.1:8762/api/account/user?userName=admin ---- get
     */
    @GetMapping("/user")
    public User selectUserByUserName(@RequestParam String userName) {
        return userService.selectUserByUserName(userName);
    }

    /**
     * 127.0.0.1/api/account/users?userName=admin&email=1111@qq.com ---- get
     */
    @GetMapping("/users")
    public List<User> getUserByUserNameOrEmail(String userName, String email) {
        return userService.getUserByUserNameOrEmail(userName, email);
    }
}
